import StoreBase, { IStoreBaseMeta } from '../../common/stores/StoreBase';
import { observable, autorun, action, computed } from 'mobx';
import {
  observablePromise,
  ObservableTempState,
  observableViewModel,
} from 'legions/store-utils';
import UiStoreBase from '../../common/stores/ui/UiStoreBase';
import {
  ComponentViewResource,
  IResourceEvent,
  ITriggerEventPrams,
} from './even/resourceEvent';
import { InstanceProForm } from 'legions-pro-design/es/LegionsProForm/interface';
import { InstanceProModal } from 'legions-pro-design/es/LegionsProModal/interface';
import { InstanceProTable } from 'legions-pro-design/es/LegionsProTable/interface';
import { TableColumnConfig } from 'legions-pro-design/es/interface/antd';
import { StoreModules } from 'legions/store';
import { ITableBaseConfigCode, SuitTuple } from '../interface/project';
import { TableListViewDomain } from '../services/components/tableListViewDomain';
import has from 'lodash/has';
import set from 'lodash/set';
// tslint:disable-next-line: no-empty-interface
import { OperationEnum } from '../constants/enum-data';
interface IContext {}

type TypeTableAutoQuerystaticMember = keyof typeof TableListViewDomain.tableAutoQuerystaticMember;
type TypeEditConfigParameter = TypeTableAutoQuerystaticMember |'customColumnsRender';

export interface IOperationConfigCode {
  onClick?: () => void;
  type: SuitTuple[number];
  btnType: string;
  btnIcon: string;
  name: string;
  nameEn: string;
  apiUrl: string;
  id: string;
  onClickCode?: string;
  params?: { key: string; value: string }[];
  method?: 'get' | 'post';
  isNeedApi?: boolean;
}
class ViewModel {

  /** * 当前活动组件key
   *
   * @type {string}
   * @memberof ViewModel
   */
  @observable currComponentKey: string = '';

  
  /*** 列表区域拖拽组件数量(泛指table数量)
   *
   * @memberof ViewModel
   */
  @observable contentDraggerList: string[] = [];

  /** * table表格属性是否编辑状态
   *
   * @type {boolean}
   * @memberof ViewModel
   */
  @observable isEditByTable: boolean = false;

  /*** 动态添加进来table 列数据，该数据用于生成代码
   *
   * @type {TableColumnConfig<{}>[]}
   * @memberof ViewModel
   */
  @observable tableColumns: TableColumnConfig<{}>[] = [];


  /**** 表格数据源配置信息
   * @type {({
   *         apiUrl: string;
   *         method: 'get' | 'post';
   *         options: Object;
   *         transform: string;
   *         model: string;
   *     })}
   * @memberof ViewModel
   */
  @observable tableDataSourceCode: {
    apiUrl: string;
    method: 'get' | 'post';
    options: string;
    transform: string;
    model: string;
    onSearch: string;
    onReset: string;
    token: string;
    parames: string;
  } = null;

  /**
   * 表格基础配置信息生成代码
   *
   * @type {ITableBaseConfigCode}
   * @memberof ViewModel
   */
  @observable tableBaseConfigCode: ITableBaseConfigCode = {
    isExportCurrData: 'false',
    isOpenCustomColumns: 'true',
    isOpenRowChange: 'true',
    tableModulesName: '',
    uniqueKey: '',
    token:'',
    columns:[],
    /* token:'', */
  };
  /*** 穿梭框显示table 列信息*/
  @observable transferShowColumns: TableColumnConfig<{}>[] = [];

  /*** 已经删除的table 列数据*/
  @observable deletedTableColumns: TableColumnConfig<{}>[] = [];

  /*** 操作区域组件属性设置结果*/
  @observable operationConfigCode: IOperationConfigCode[] = [];

  /*** 当前正在编辑的操作按钮id 信息*/
  @observable openOperationConfigId: string = '';
  /** 搜索条件可执行表达式 */
  @observable paramesExeccodeast = null;
  @observable tranformExeccodeast = null;
  @computed get computedLabelWidth() {
    return 0;
  }
}
class tableAutoQueryVM{
  /** 代码编辑器值 */
  @observable code = '';

  /** 当前正在编辑的自动托管配置项参数 */
  @observable editConfigParameter:  TypeEditConfigParameter= null;

  /** 表格列数据编辑时选中的编辑行数据 */
  @observable sourceSelectedKeys: string[] = [];

  /** 对话框内容区展示组件类型，主要用于自动托管代码编辑及自定义列函数代码 */
  @observable operationEnum: (typeof OperationEnum)[keyof typeof OperationEnum] = null;

  @action clear(name: ['code' | 'editConfigParameter']) {
    name.map((item) => {
        if(has(this,item)){
          set(this,item,'')
        }
    })
  }
}
@StoreModules
export default class ProjectTablePropertyStore extends UiStoreBase<IContext> {
  static meta: IStoreBaseMeta = {
    ...StoreBase.meta,
    eventScopes: [ComponentViewResource],
    contextTypes: {},
  };
  viewModel = observableViewModel<ViewModel>(new ViewModel());
  /** 表格接口自动托管配置参数Model */
  @observable tableAutoQueryVM = observableViewModel<tableAutoQueryVM>(new tableAutoQueryVM())
  /** 表格实时预览实例 */
  tablePreviewRef: InstanceProTable = null;

  /** 表格组件属性设置页面对话框实例 */
  tablePropertyModalRef: InstanceProModal = null;

  /*** 操作按钮区属性设置Modal */
  operationModalRef: InstanceProModal = null;

  /** 表格组件属性配置表单侧边对话框实例 */
  tablePropertyDrawerModalRef: InstanceProModal = null;
  constructor(context: IContext) {
    super(context);
  }
  @action onEvent(event: IResourceEvent<ITriggerEventPrams>) {
  }
}
